Dart Set operator ==
Syntax & Examples


Set.operator == operator

The `==` operator in Dart is used to test for equality between two objects.


Syntax of Set.operator ==

The syntax of Set.operator == operator is:

operator ==(dynamic other) → bool

This operator == operator of Set the equality operator.

Parameters

ParameterOptional/RequiredDescription
otherrequiredThe object to compare for equality with the current object.


✐ Examples

1 Check equality of identical sets

In this example,

  1. We create two sets set1 and set2 with the same elements in the same order.
  2. We use the == operator to compare if the sets are equal.
  3. We print the result to standard output.

Dart Program

void main() {
  Set<int> set1 = {1, 2, 3};
  Set<int> set2 = {1, 2, 3};
  bool result = set1 == set2;
  print('Are sets equal? $result');
}

Output

Are sets equal? true

2 Check equality of different sets

In this example,

  1. We create two sets set1 and set2 with the same elements but in different order.
  2. We use the == operator to compare if the sets are equal.
  3. We print the result to standard output.

Dart Program

void main() {
  Set<int> set1 = {1, 2, 3};
  Set<int> set2 = {1, 3, 2};
  bool result = set1 == set2;
  print('Are sets equal? $result');
}

Output

Are sets equal? false

Summary

In this Dart tutorial, we learned about operator == operator of Set: the syntax and few working examples with output and detailed explanation for each example.